package com.wenhua.junshao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenhua.junshao.common.R;
import com.wenhua.junshao.entity.Employee;
import com.wenhua.junshao.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     */
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //1从requestBody中查询出来用户名和密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //1.1   将password进行加密
        String password2 = DigestUtils.md5DigestAsHex(password.getBytes());


        //2依据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, username);
        Employee emp = employeeService.getOne(queryWrapper);

        if (emp == null) {
            return R.error("用户名不存在");
        }

        if (!emp.getPassword().equals(password2)) {

            return R.error("密码错误");

        }
        if (emp.getStatus() == 0) {
            return R.error("账号禁用");
        }

        //将id存入session中
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //销毁session
        request.getSession().invalidate();
        return R.success("退出成功");

    }


    /**
     * 新增员工
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("新增员工,员工信息为:{}", employee.toString());
        //设置新员工的初始密码为123456

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置更新修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //获取当前登录人的id
        Long empId = (Long) request.getSession().getAttribute("employee");
        //完善修改人和创建人的信息
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     */
    @GetMapping("page")

    public R<Page<Employee>>

    page(Integer page, Integer pageSize, String name) {

        log.info("page={},pageSize={},name={}", page, pageSize, name);

        //构造分页器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getName);
        //执行查询
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);

    }

    /**
     * 根据id修改员工信息
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        log.info(employee.toString());

//        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}



